#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<stack>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=5e5+1000;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL a[maxn];
LL l[maxn],r[maxn];
LL pre[maxn],suf[maxn];
int main(void){
   cin.tie(0);std::ios::sync_with_stdio(false);
   LL n;cin>>n;
   for(LL i=1;i<=n;i++){
      cin>>a[i];
   }
   stack<LL>s;
   a[0]=a[n+1]=0;
   for(LL i=1;i<=n;i++){
      while(!s.empty()&&a[s.top()]>=a[i]) s.pop();
      if(!s.empty()) l[i]=s.top();
      else l[i]=0;
      s.push(i);
   }
   while(!s.empty()) s.pop();
   for(LL i=n;i>=1;i--){
      while(!s.empty()&&a[s.top()]>=a[i]) s.pop();
      if(!s.empty()) r[i]=s.top();
      else r[i]=n+1;
      s.push(i);
   }
   for(LL i=1;i<=n;i++){
       pre[i]=pre[l[i]]+(i-l[i])*a[i];
   }
   for(LL i=n;i>=1;i--){
       suf[i]=suf[r[i]]+(r[i]-i)*a[i];
   }
   LL ans=0;LL pos=0;
   for(LL i=1;i<=n;i++){
      if(pre[i]+suf[i]-a[i]>ans){
         ans=pre[i]+suf[i]-a[i];
         pos=i;
      }
   }
   vector<LL>v1,v2;
   LL h1=a[pos];
   v1.push_back(h1);
   for(LL i=pos-1;i>=1;i--){
      if(a[i]>=h1){
        v1.push_back(h1);
      }
      else if(a[i]<h1){
        h1=a[i];
        v1.push_back(h1);
      }
   }
   reverse(v1.begin(),v1.end());
   h1=a[pos];
   for(LL i=pos+1;i<=n;i++){
      if(a[i]>=h1){
        v2.push_back(h1);
      }
      else if(a[i]<h1){
        h1=a[i];
        v2.push_back(h1);
      }
   }
   for(auto i:v1){
     cout<<i<<" ";
   }
   for(auto i:v2){
     cout<<i<<" ";
   }cout<<"\n";
   return 0;
}
